package com.lebang.service;

import com.alibaba.fastjson.JSON;
import com.lebang.component.RedisClient;
import com.lebang.entity.RequestHeaders;
import com.lebang.entity.SdkVideoBaidu;
import com.lebang.mapper.SdkVideoBaiduMapper;
import com.lebang.model.enums.SdkVideoEnums;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.mongodb.LookVideoRecordMongodb;
import com.lebang.service.sys.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

import static com.lebang.controller.api.SdkUploadApi.MAX_VIDEO_COUNT;
import static com.lebang.service.TaskInRedisService.HASH_CSJ;

/**
 * @Author: hwq
 * @Date: 2019/11/5 17:31
 * @Version 1.0
 */
@Service
public class SdkVideoService {
    @Resource
    private SdkVideoBaiduMapper baiduMapper;
    @Autowired
    private DictService dictService;
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private PointService pointService;
    @Autowired
    private LookVideoRecordMongodb lookVideoRecordMongodb;

    public boolean creat(String memberId, int money, SdkVideoEnums sdkVideoEnums, RequestHeaders requsetHeader) {
        SdkVideoBaidu baidu = new SdkVideoBaidu();
        baidu.setCreateTime(new Date());
        baidu.setModifyTime(new Date());
        baidu.setMoney(new BigDecimal(money));
        baidu.setEvent("看视频收入" + money + "金币");
        baidu.setType(1);
        baidu.setMemberId(memberId);
        baidu.setSdkId(sdkVideoEnums.getId());
        baidu.setOrderNum(String.valueOf(System.currentTimeMillis()));
        baidu.setAppId(requsetHeader.getAppid());
        baidu.setMyMoney(new BigDecimal("0"));
        baidu.setParam(JSON.toJSONString(requsetHeader));
        lookVideoRecordMongodb.save(baidu);
        switch (sdkVideoEnums) {
            case BAIDU:
                pointService.addPointBySdk(memberId, money, TaskCategory.BAIDUSP, baidu.getEvent(),requsetHeader);
            case GDT:
                pointService.addPointBySdk(memberId, money, TaskCategory.GDT, baidu.getEvent(),requsetHeader);
                break;
            case KUAISHOU  :
                pointService.addPointBySdk(memberId, money, TaskCategory.KUAI_SHOU, baidu.getEvent(),requsetHeader);
                break;
            case CSJ:
                pointService.addPointBySdk(memberId, money, TaskCategory.CHUANGSHANJIA, baidu.getEvent(),requsetHeader);
                break;
            case SIGMOB:
                pointService.addPointBySdk(memberId, money, TaskCategory.SIGMOB, baidu.getEvent(),requsetHeader);
                break;
            case LZ_YD:
                pointService.addPointBySdk(memberId, money, TaskCategory.LIZI_YD, baidu.getEvent(),requsetHeader);
                break;
            default:
        }
        return true;

    }

    public Long countByExample( String sdkId, Date star, Date end) {
        SdkVideoBaidu sdkVideoBaidu= new SdkVideoBaidu();
        sdkVideoBaidu.setSdkId(sdkId);
        ExampleMatcher exampleMatcher=ExampleMatcher.matching().withMatcher("sdkId", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<SdkVideoBaidu> example=Example.of(sdkVideoBaidu,exampleMatcher);
        return lookVideoRecordMongodb.count(example);
    }

    public Page<SdkVideoBaidu> pageByExample(String sdkId, Long page, Integer limit, Date star, Date end) {
//        SdkVideoBaiduExample example1=new SdkVideoBaiduExample();
//        example1.setLimit(500);
//        List<SdkVideoBaidu> list=baiduMapper.selectByExample(example1);
//        lookVideoRecordMongodb.saveAll(list);

        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page.intValue(), limit, sort);
        SdkVideoBaidu sdkVideoBaidu= new SdkVideoBaidu();
        //sdkVideoBaidu.setSdkId(sdkId);
        ExampleMatcher exampleMatcher=ExampleMatcher.matching().withMatcher("sdkId", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<SdkVideoBaidu> example=Example.of(sdkVideoBaidu,exampleMatcher);
        return lookVideoRecordMongodb.findAll(example,pageable);
    }

    /**
     * 判断该用户是否看视频次数超过了最大值
     *
     * @param memberId
     * @return
     */
    public boolean checkMaxVideoCount(String memberId) {
        int o = dictService.selectIntByKeyName(MAX_VIDEO_COUNT);
        int count = 0;
        String obj = redisClient.hget(HASH_CSJ, memberId);
        if (obj != null) {
            count = Integer.parseInt(obj);
        }
        return count > o;
    }
}
